package com.lizi.apidoc.handler;

import com.lizi.apidoc.constants.DocGlobal;
import com.lizi.apidoc.constants.DocTags;
import com.lizi.apidoc.model.ApiObjectProperty;
import com.lizi.apidoc.model.ApiReturn;
import com.lizi.apidoc.utils.DocClassUtil;
import com.lizi.apidoc.utils.JavaClassValidateUtil;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaMethod;

import java.util.List;

/**
 * Response Param Handler
 *
 * @author kingman zhangjw@3vjia.com
 * @since 2020-05-07 12:49
 */
public class ResponseParamHandler extends ParamHandler {

    public static ResponseParamHandler builder() {
        return new ResponseParamHandler();
    }

    public ApiObjectProperty handle(final JavaMethod javaMethod, String comment) {
        if (javaMethod.getReturns().isVoid()) {
            return null;
        }
        ApiReturn apiReturn = DocClassUtil.processReturnType(javaMethod.getReturnType().getGenericCanonicalName());
        String returnType = apiReturn.getGenericCanonicalName();
        String typeName = apiReturn.getSimpleName();

        if (this.ignoreReturnObject(typeName)) {
            return null;
        }

        List<DocletTag> paramTags = javaMethod.getTagsByName(DocTags.RETURN);
        ApiObjectProperty apiObjectProperty = getApiParams("",returnType,typeName,"",comment,false);

        return apiObjectProperty;

    }

    private boolean ignoreReturnObject(String typeName) {
        if (JavaClassValidateUtil.isMvcIgnoreParams(typeName)) {
            return DocGlobal.MODE_AND_VIEW_FULLY.equals(typeName);
        }
        return false;
    }
}
